<% layout('/layouts/default.html', {title: '预收款管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(tadvancesReceived.isNewRecord ? '新增预收款' : '编辑预收款')}
		</div>
		<#form:form id="inputForm" model="${tadvancesReceived}" action="${ctx}/prerecieve/tadvancesReceived/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="advancesReceivedId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="advancesReceivedCode" maxlength="100" class="form-control"  readonly="true" placeholder="自动生成"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="customerId" items="${customerList}" itemLabel="customerName" itemValue="customerId" dictType="" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('日期')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="date" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
							</div>
						</div>
					</div>

					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('服务订单')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="serviceOrderId" items="${serviceOrderList}" itemLabel="serviceOrderCode" itemValue="serviceOrderId" onchange="changeNumber()" class="form-control required" blankOption="true"  />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('收款名字')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="receivedName" maxlength="100" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('币种')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="currency" dictType="sys_bizhongy" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('单据状态')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="status1" dictType="receivable_bill_type" class="form-control required" readonly="true" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('审批状态')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="billStatus" dictType="finance_bill_status" class="form-control required" readonly="true" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('预收款余额')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="advancesReceivedBalance" class="form-control number" readonly="true"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('前置单据类型')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:select path="parentBillType" rows="4" maxlength="500" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>

					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('前置单据编号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="parentBillType" rows="4" maxlength="500" class="form-control" readonly="true"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				</div>

				<h4 class="form-unit">${text('预计收款明细')}</h4>
				<div class="ml10 mr10">
					<table id="tadvancesReceivedEstimatedDataGrid"></table>
					<% if (hasPermi('prerecieve:tadvancesReceived:edit')){ %>
						<a href="#" id="tadvancesReceivedEstimatedDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
				<h4 class="form-unit">${text('实际收款明细')}</h4>
				<div class="ml10 mr10">
					<table id="tadvancesReceivedActualDataGrid"></table>
					<!--<% if (hasPermi('prerecieve:tadvancesReceived:edit')){ %>
						<a href="#" id="tadvancesReceivedActualDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>-->
				</div>
				<h4 class="form-unit">${text('系统信息')}</h4>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('创建者')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="createBy" maxlength="64" class="form-control" disabled="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('创建时间')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="createDate" readonly="true" maxlength="20" class="form-control" disabled="true"
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('更新者')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="updateBy" maxlength="64" class="form-control" disabled="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('更新时间')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="updateDate" readonly="true" maxlength="20" class="form-control" disabled="true"
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
						</div>
					</div>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('prerecieve:tadvancesReceived:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//初始化预计收款明细DataGrid对象
$("#tadvancesReceivedEstimatedDataGrid").dataGrid({

	data: ${toJson(tadvancesReceived.tadvancesReceivedEstimatedList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'advancesReceivedEstimatedId', editable:true, hidden:true},
		{header:'${text("行号")}', name:'rowNumber', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control required',
				items: ${toJson(rowNumber)},
				itemLabel: 'rowNumber', itemValue: 'rowNumber',
				dataInit: function(element){
					$(element).select2().on("change",function(){
						if($(this).val()==""){

							$(this).closest('tr').find('[aria-describedby="tadvancesReceivedEstimatedDataGrid_frameNumber"] .form-control').val("").trigger('change');
							$(this).closest('tr').find('[aria-describedby="tadvancesReceivedEstimatedDataGrid_carName1"] .form-control').val("").trigger('change');

							return;
						}
						//订单号
						var serviceOrderId =$("#serviceOrderId").val();
						$(this).resetValid();

						var number ;
						var name;
						$.ajax({
							type: "POST",
							async : false,
							url: "${ctx}/payment/tap/getCarDetailInfo1",
							data: {"rowNumber": $(this).val(),"serviceOrderId":serviceOrderId},
							success: function (data) {
								name = data.tcar.carName;

								number=data.frameNumber;
							}
						});
						$(this).closest('tr').find('[aria-describedby="tadvancesReceivedEstimatedDataGrid_frameNumber"] .form-control').val(number).trigger('change');
						$(this).closest('tr').find('[aria-describedby="tadvancesReceivedEstimatedDataGrid_carName1"] .form-control').val(name).trigger('change');

					});
				}
			}
		},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control','readonly':'true'}},
		{header:'${text("车辆名称")}', name:'carName1', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control','readonly':'true'}},
		{header:'${text("预收款类型")}', name:'advancesReceivedType', width:150,
			editable:true, edittype:'select', editoptions:{'class':'form-control required',
				dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid();

					});
				}
			}
		},
		{header:'${text("预计收款金额")}', name:'estimatedReceivedAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number required'}},
		{header:'${text("预收款ID")}', name:'advancesReceivedId.advancesReceivedId', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
				var actions = [];
				if (val == 'new'){
					actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#tadvancesReceivedEstimatedDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				}else{
					actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#tadvancesReceivedEstimatedDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
				}
				return actions.join('');
			}, editoptions: {defaultValue: 'new'}}
	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#tadvancesReceivedEstimatedDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {advancesReceivedEstimatedId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'tadvancesReceivedEstimatedList', // 提交的数据列表名
	editGridInputFormListAttrs: 'rowNumber,status,carName1,advancesReceivedEstimatedId,receivedSumActual,receivedAmountActual,receivedAmountEstimated,frameNumber,advancesReceivedType,estimatedReceivedAmount,advancesReceivedId.advancesReceivedId,', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
//初始化实际收款明细DataGrid对象
$("#tadvancesReceivedActualDataGrid").dataGrid({

	data: ${toJson(tadvancesReceived.tadvancesReceivedActualList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	
	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'advancesReceivedActualId', editable:true, hidden:true},
		{header:'类型', name:'advancesReceivedType', edittype:'text',editable:true, hidden:true},
		{header:'方式', name:'advancesReceivedMethod', edittype:'text',editable:true, hidden:true},
		{header:'银行', name:'advancesReceivedBank', edittype:'text',editable:true, hidden:true},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control'}},
		{header:'${text("预收款类型")}', name:'advancesReceivedType1', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control'}},
		{header:'${text("收款方式")}', name:'advancesReceivedMethod1', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control'}},
		{header:'${text("收款银行")}', name:'advancesReceivedBank1', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control'}},
		{header:'${text("实际预收款金额")}', name:'actualAdvancesReceivedAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("收款日期")}', name:'date', width:150, 
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
			}
		},
		{header:'${text("预收款ID")}', name:'advancesReceivedId.advancesReceivedId', editable:true, hidden:true},

	],
	
	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#tadvancesReceivedActualDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {advancesReceivedActualId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据
	
	// 编辑表格的提交数据参数
	editGridInputFormListName: 'tadvancesReceivedActualList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,advancesReceivedActualId,advancesReceivedType,frameNumber,advancesReceivedMethod,advancesReceivedBank,actualAdvancesReceivedAmount,date,createBy,createDate,advancesReceivedId.advancesReceivedId,', // 提交数据列表的属性字段
	
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
</script>

<script>
	//费用项目
	$(function () {
		var str="";
		$.ajax({
			type: "POST",
			async : false,
			url: "${ctx}/payment/tap/getExpenseItem",
			// data: {"serviceOrderId": $("#serviceOrderId").val()},
			dataType : "json",
			success: function (data) {
				// $('#currency').select2('val', data.currency);
				//根据订单编号赋值车架号
				if (data.length > 0) {
					for (var i = 0; i < data.length; i++) {
						var frameNumber1 = data[i].fixedExpenseName;

						var frameNumberId = data[i].fixedExpenseId;
						if(i!=data.length-1){
							str+=frameNumberId+":"+frameNumber1+";";
						}else {
							str+=frameNumberId+":"+frameNumber1;// 这里是option里面的 value:label
						}
					}

				} else{

				}
				$('#tadvancesReceivedEstimatedDataGrid').setColProp('advancesReceivedType', {editoptions: {value:str}});
				$('#tadvancesReceivedEstimatedDataGrid').dataGrid('reloadGrid');

			}
		});

	})

function changeNumber() {
	var str=":;";
	var serviceOrderId =$("#serviceOrderId").val();
	$.ajax({
		type: "post",
		url:"${ctx}/payment/tap/getNumberList1",
		data:{"serviceOrderId":serviceOrderId},
		success: function(data){
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					var row = data[i];

					if (i != data.length - 1) {
						str += row + ":" + row + ";";
					} else {
						str += row + ":" + row;// 这里是option里面的 value:label
					}
				}
			}
			$('#tadvancesReceivedEstimatedDataGrid').setColProp('rowNumber', {editoptions: {value:str}});
			$('#tadvancesReceivedEstimatedDataGrid').dataGrid('reloadGrid');
		}
	});
}
$(function () {
	$("#date").attr("value",js.formatDate(new Date()
			, 'yyyy-MM-dd'));
})

</script>